//
//  TestOneController.swift
//  SwiftDemo
//
//  Created by 宏泰财富 on 2018/10/31.
//  Copyright © 2018年 宏泰财富. All rights reserved.
//

import UIKit

class TestOneController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.setUpUI()
    }
    
    func setUpUI(){
        let btn: UIButton = UIButton(type: .custom)
        btn.frame = CGRect(x: 50, y: 200, width: 200, height: 100)
        btn.backgroundColor = UIColor.orange
        btn.setTitle("test", for: .normal)
        btn.addTarget(self, action: #selector(TestOneController.btnClick(_:)), for: .touchUpInside)
        self.view.addSubview(btn)
    }
    

    open func testfun(oneStr:String, twoStr:String)->String{
        let oneTwo = oneStr + twoStr;
        return oneTwo;
    }
    
    private func questionMarkUse(){
        let optionalStr: String? = "hello";
        print(optionalStr == nil)
        let optionalName: String? = "John Appleaseed"
        var greeting = "Hello!"
        if let name = optionalName{
            greeting = "Hello, \(name)"
            print(greeting);
        }
    }
    
    func test1() {
        let vegetable = "red pepper"
        switch vegetable {
        case "celery":
            let vegetableComment = "Add some raisins and make ants on a log"
            print(vegetableComment)
        case "cucumber", "watercress":
            let vegetableComment = "That would make a good tea sandwich."
            print(vegetableComment)
        case let x where x.hasSuffix("pepper"):
            let vegetableComment = "Is it a spicy \(x)?"
            print(vegetableComment)
        default:
            let vegetableComment = "Everything tastes good in soup."
            print(vegetableComment)
        }
    }
    
    func test2() {
        let interestingNumbers = [
            "Prime": [2, 3, 5, 7, 11, 13],
            "Fibonacci": [1, 1, 2, 3, 5, 8],
            "Square": [1, 4, 9, 16, 25],
        ]
        var largest = 0
        for (kind, numbers) in interestingNumbers {
            print(kind);
            for number in numbers{
                if number > largest{
                    largest = number;
                }
            }
        }
        print(largest)
    }
    
    func test3(){
        var n = 2
        while n < 100 {
            n = n*2
        }
        print(n)
        var m = 2
        repeat{
            m = m*2
        }while m < 100
        print(m)
    }
    
    func test4(){
        let x = [1,2,3]
        var y = x
        y.append(4)
        print("x的值\(x)\n")
        print("y的值\(y)\n")
        print("x的地址\(String(format: "%p", x))")
        print("y的地址\(String(format: "%p", y))")
    }
    
    func test5(){
        let a: NSMutableArray = [1,2,3]
        let b: NSArray = a
        a.insert(4, at: 3)
        print("a的值\(a)\n")
        print("b的值\(b)\n")
        print("a的地址\(String(format: "%p", a))")
        print("b的地址\(String(format: "%p", b))")
    }
    
    func test6() {
        let strArr: [String?] = ["ab", "cc" , nil, "dd"]
        let mapResult = strArr.map{ $0 }
//        let filterResult: [String?] = strArr.filter{$0}
        let str2Arr: [String?] = ["ab", "cc" , nil, "dd"]
        var faltMapREsult = str2Arr.flatMap{ $0 }
        print("mapResult\(mapResult)")
        print("faltMapResult\(faltMapREsult)")
        
        let optionalArray: [String?] = ["AA", nil, "BB", "CC"];
        var optionalResult = optionalArray.flatMap{ $0 }
        print(optionalResult)
    }
    
    func test7(){
        
        var personArr = [Person]()
        
        let person1: Person = Person()
        person1.name = "张三"
        person1.age = 15
        let person2: Person = Person()
        person2.name = "王五"
        person2.age = 18
       
        let person3: Person = Person()
        person3.name = "李四"
        person3.age = 16
        
        let person4: Person = Person()
        person4.name = "赵六"
        person4.age = 13
        
        personArr.append(person1)
        personArr.append(person2)
        personArr.append(person3)
        personArr.append(person4)
        
        print("personArr的结果是")
        for person in personArr {
            print("\(String(describing: person.name)) \n \(person.age)")
        }
      
        let result = personArr.sorted {(a, b) -> Bool in
            return a.age > b.age
        }

        print("排序后的结果")
        for person in result {
            print("\(String(describing: person.name)) \n \(person.age )")
        }
        
    }
    
    func test8() {
        let names = ["Paula", "Elana", "Zoe"]
        var lastNameEndingInA: String?
        for name in names.reversed() where name.hasSuffix("a") {
            lastNameEndingInA = name
            break
        }
        print("description of lastNameEndingInA: \(lastNameEndingInA as String?)")
        
        let match = names.last {$0.hasSuffix("a")}
        print("\(match as String?)")
    
    }
    
    func test9() {
       let result = [1,2,3,4].accumulate(2, *)
       print(result)
    }
    
    func test10() {
        
    }
}

extension TestOneController{
    @objc fileprivate func btnClick(_: UIButton){
        test9()
    }
}
